Add gdk_texture_new_from_bytes
authorMatthias Clasen <mclasen@redhat.com>
Mon, 13 Sep 2021 17:30:12 +0000 (13:30 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 16 Sep 2021 02:00:09 +0000 (22:00 -0400)
Add this new api, and make gdk_texture_new_from_file
a wrapper around it.

gdk/gdktexture.c
gdk/gdktexture.h

index 7b055238fd39084ed640e83351996935b5b128c9..ee7570e7e7a479187341a6ec998e3acaf4cbc8c0 100644 (file)
@@ -383,19 +383,55 @@ GdkTexture *
 gdk_texture_new_from_file (GFile   *file,
                            GError **error)
 {
+  GBytes *bytes;
   GdkTexture *texture;
-  GdkPixbuf *pixbuf;
-  GInputStream *stream;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  stream = G_INPUT_STREAM (g_file_read (file, NULL, error));
-  if (stream == NULL)
+  bytes = g_file_load_bytes (file, NULL, NULL, error);
+  if (bytes == NULL)
     return NULL;
 
+  texture = gdk_texture_new_from_bytes (bytes, error);
+
+  g_bytes_unref (bytes);
+
+  return texture;
+}
+
+
+/**
+ * gdk_texture_new_from_bytes:
+ * @bytes: a `GBytes` containing the data to load
+ * @error: Return location for an error
+ *
+ * Creates a new texture by loading an image from memory,
+ *
+ * The file format is detected automatically. The supported formats
+ * are PNG and JPEG, though more formats might be available.
+ *
+ * If %NULL is returned, then @error will be set.
+ *
+ * Return value: A newly-created `GdkTexture`
+ *
+ * Since: 4.6
+ */
+GdkTexture *
+gdk_texture_new_from_bytes (GBytes  *bytes,
+                            GError **error)
+{
+  GInputStream *stream;
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture;
+
+  g_return_val_if_fail (bytes != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  stream = g_memory_input_stream_new_from_bytes (bytes);
   pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
   g_object_unref (stream);
+
   if (pixbuf == NULL)
     return NULL;
 
index 9e3b7a93fa3d74bd9822c7057d6ece7cc1df4bfe..7cccdb592831e8b5542580bb55cd8c7988d5214e 100644 (file)
@@ -49,6 +49,9 @@ GdkTexture *            gdk_texture_new_from_resource          (const char
 GDK_AVAILABLE_IN_ALL
 GdkTexture *            gdk_texture_new_from_file              (GFile           *file,
                                                                 GError         **error);
+GDK_AVAILABLE_IN_4_6
+GdkTexture *            gdk_texture_new_from_bytes             (GBytes          *bytes,
+                                                                GError         **error);
 
 GDK_AVAILABLE_IN_ALL
 int                     gdk_texture_get_width                  (GdkTexture      *texture) G_GNUC_PURE;